package com.windmill.cmd.game;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.HashMap;
import java.util.Map;

public class ABGuess implements GuessNumberGame {
	public static void main(String[] args) {
		ABGuess a = new ABGuess();
		System.out.println(a.guess(7006,"7060"));
	}
    @Override
    public Map<String,String> guess(Integer right,String guess) {
	Map<String,String> result = new HashMap<String,String>();
	char[] r = format(right).toCharArray();
	guess = format(Integer.parseInt(guess)).substring(0,r.length);
	char[] g = guess.toCharArray();
	boolean[] gf = new boolean[4];
	boolean[] rf = new boolean[4];
	int a = 0;
	int b = 0;
	for(int i=0;i<r.length;i++){
		if(g[i]==r[i]&&!gf[i]){
			a++;
			if(rf[i]) b--;
			gf[i] = true;
			rf[i] = true;
		}
		for(int j=0;j<r.length;j++){
			if(g[i]==r[j]&&!rf[j]&&!gf[i]){
				b++;
				rf[j] = true;
				gf[i] = true;
				break;
			}
		}
	}
	String sendMsg = " ☆您猜的数字匹配为【"+guess+"："+a+"A"+b+"B】";
	result.put("message", sendMsg);
	if(a==r.length){	    
	    result.put("result","1");
	}else{
	    result.put("result","0");
	}
	return result;
    }
    private String format(int guess){
	Format fmt = new DecimalFormat("0000");
    return fmt.format(guess);
}
}
